//
//  ZYNetWorkTools.swift
//  weiboZY
//
//  Created by 赵勇 on 16/5/12.
//  Copyright © 2016年 赵勇. All rights reserved.
//

import UIKit
import AFNetworking

// 网络请求方式
enum RequestType: Int {
   case GET = 0
   case POST = 1
}

class ZYNetWorkTools: AFHTTPSessionManager {
    // 单例
     static let sharedTool:ZYNetWorkTools = {
        let tools = ZYNetWorkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    // 网络请求封装
    
    func request(requestType:RequestType, url:String, params:AnyObject?, callBack:((response:AnyObject?, error: NSError?)->()) ) {
        // 方法二
        // 定义一个成功的闭包
        let successClosure = { (dataTask:NSURLSessionDataTask, response:AnyObject?) -> Void in
            callBack(response: response, error: nil)
        }
        // 定义一个失败的闭包
        let failureClosure = { (dataTask:NSURLSessionDataTask?, error:NSError) -> Void in
            callBack(response: nil, error: error)
         
        }
        if requestType == .GET {
            // get请求
             GET(url, parameters: params, progress: nil, success: successClosure, failure: failureClosure)
            
        } else {
            // post请求
             POST(url, parameters: params, progress: nil, success: successClosure, failure: failureClosure)
        }
       
        
       
        
        
        // 方法一
//        if requestType == .GET {
//            
//              GET(url, parameters: params, progress: nil, success: { (_, response) -> Void in
//                callBack(response: response, error: nil)
//                }, failure: { (_, error) -> Void in
//                    callBack(response: nil, error: error)
//              })
//        } else {
//            
//             POST(url, parameters: params, progress: nil, success: { (_, response) -> Void in
//                callBack(response: response, error: nil)
//                }, failure: { (_, error) -> Void in
//                    callBack(response: nil, error: error)
//             })
//        }
        
      
       
    }
    
    
    
}

// 发送微博接口
extension ZYNetWorkTools {
    
    // 发送图片微博的接口
    
    func upload(access_token:String, status:String, image:UIImage, callBack: ((response:
        
        AnyObject?, error: NSError?) -> ()) ) {
            
            let url = "https://upload.api.weibo.com/2/statuses/upload.json"
            
        let params = [
                
                "access_token":access_token,
                "status":status
            ]
            
            let data = UIImageJPEGRepresentation(image, 0.6)!
            
            
//        POST(url, parameters: params, constructingBodyWithBlock: { (formData) -> Void in
//            
//          formData.appendPartWithFileData(data, name: "pic", fileName: "test", mimeType: "image/jpeg")
//
//            }, success: { (_, response) -> Void in
//                
//                callBack(response: response, error: nil)
//                
//            }) { (_, error) -> Void in
//                
//                callBack(response: nil, error: error)
//                    }
        POST(url, parameters: params, constructingBodyWithBlock: { (formData) -> Void in
            formData.appendPartWithFileData(data, name: "pic", fileName: "text", mimeType: "image/jpeg")
            }, progress: nil, success: { (_, response) -> Void in
                
                callBack(response: response, error: nil)
            }) { (_, error) -> Void in
                
                callBack(response: nil, error: error)
            }
            
    }
    
    
    
    // 请求微博首页数据
    func sendUpdate(access_token:String, status:String, callBack: ((response:
        
        AnyObject?, error: NSError?) -> ()) ) {
            let url = "https://api.weibo.com/2/statuses/update.json"
            
            let params = [
                "access_token":access_token,
                "status":status
            ]
            
            request(.POST, url: url, params: params, callBack: callBack)
    }
    
    
}


// 首页相关接口
extension ZYNetWorkTools {
    
  // 请求微博首页数据
    func requestStatus(access_token:String, maxId: Int64 = 0, sinceId: Int64 = 0, callBack: ((response:
        
        AnyObject?, error: NSError?) -> ()) ) {
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        
        let params = [
            "access_token":access_token,
            "max_id":"\(maxId)",
            "since_id":"\(sinceId)"
        ]

        request(.GET, url: url, params: params, callBack: callBack)
    }
    
}



// 延展
// OAuth相关接口网络请求
extension ZYNetWorkTools {
    
    // 通过code获取accessToken
    func requestAccessToken(code:String, callBack: ((response: AnyObject?, error: NSError?) -> ()) ) {
        
        let url = "https://api.weibo.com/oauth2/access_token"
        let params = [
            "client_id": weiboAppKey,
            "client_secret": weiboAppSecret,
            "grant_type": "authorization_code",
            "code":code,
            "redirect_uri": weiboRedirect_Uri
        ]
        
       request(.POST, url: url, params: params, callBack: callBack)
    
   
       }

    
    
    //  通过accessToken获取用户个人信息
       func requestUserInfo(userAcount:ZYUserAcount, callBack: ((response: AnyObject?, error: NSError?) -> ())) {
        
        let url = "https://api.weibo.com/2/users/show.json"
        let params = [
            "access_token":userAcount.access_token!,
            "uid":"\(userAcount.uid)"
        ]
        
        request(.GET, url: url, params: params, callBack: callBack)
    }
    
}
















